package com.gdth.core.widget;

import java.lang.reflect.Field;

import com.gdth.core.annotation.view.EventListener;
import com.gdth.core.annotation.view.Method;
import com.gdth.core.annotation.view.ViewInject;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AbsListView;

/** 
 * @author 作者 Arvin: 
 * @version 创建时间：2016年5月3日 上午11:53:25 
 * 类说明 
 */
public class AnnotationUtil {
	public static void initInjectedView(Object injectedSource, View sourceView){ 
 		Field[] fields = injectedSource.getClass().getDeclaredFields(); 
 		if(fields!=null && fields.length>0){ 
 			for(Field field : fields){ 
 				try { 
 					field.setAccessible(true); 
 					 
 					if(field.get(injectedSource)!= null ) 
 						continue; 
 				 
 					ViewInject viewInject = field.getAnnotation(ViewInject.class); 
 					if(viewInject!=null){ 
 						int viewId = viewInject.id(); 
 					    field.set(injectedSource,sourceView.findViewById(viewId)); 
 					 
 					    setListener(injectedSource,field,viewInject.click(),Method.Click); 
 						setListener(injectedSource,field,viewInject.longClick(),Method.LongClick); 
 						setListener(injectedSource,field,viewInject.itemClick(),Method.ItemClick); 
 						setListener(injectedSource,field,viewInject.itemLongClick(),Method.ItemLongClick); 
 						setListener(injectedSource,field,viewInject.touch(),Method.Touch); 

//						Select select = viewInject.select(); 
//						if(!TextUtils.isEmpty(select.selected())){ 
//							setViewSelectListener(injectedSource,field,select.selected(),select.noSelected()); 
//						} 
					} 
				} catch (Exception e) { 
					e.printStackTrace(); 
				} 
			} 
		} 
	} 

	public static void initInjectedView(View sourceView){
		Field[] fields = sourceView.getClass().getDeclaredFields();
		if(fields!=null && fields.length>0){
			for(Field field : fields){
				try {
					field.setAccessible(true);
					
					if(field.get(sourceView)!= null )
						continue;
				
					ViewInject viewInject = field.getAnnotation(ViewInject.class);
					if(viewInject!=null){
						int viewId = viewInject.id();
					    field.set(sourceView, sourceView.findViewById(viewId));
					
					    setListener(sourceView,field,viewInject.click(),Method.Click);
					    setListener(sourceView,field,viewInject.longClick(),Method.LongClick);
					    setListener(sourceView,field,viewInject.itemClick(),Method.ItemClick);
					    setListener(sourceView,field,viewInject.itemLongClick(),Method.ItemLongClick);
					    setListener(sourceView,field,viewInject.touch(),Method.Touch);
						
//						Select select = viewInject.select();
//						if(!TextUtils.isEmpty(select.selected())){
//							setViewSelectListener(injectedSource,field,select.selected(),select.noSelected());
//						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void initInjectedFragment(Fragment sourceView){
		Field[] fields = sourceView.getClass().getDeclaredFields();
		if(fields!=null && fields.length>0){
			for(Field field : fields){
				try {
					field.setAccessible(true);
					
					if (field.isAnnotationPresent(ViewInject.class)) {  
						ViewInject viewInject = field.getAnnotation(ViewInject.class);

						int viewId = viewInject.id();
					    field.set(sourceView, sourceView.getView().findViewById(viewId));
					
					    setListener(sourceView,field,viewInject.click(),Method.Click);
					    setListener(sourceView,field,viewInject.longClick(),Method.LongClick);
					    setListener(sourceView,field,viewInject.itemClick(),Method.ItemClick);
					    setListener(sourceView,field,viewInject.itemLongClick(),Method.ItemLongClick);
					    setListener(sourceView,field,viewInject.touch(),Method.Touch);
						
//						Select select = viewInject.select();
//						if(!TextUtils.isEmpty(select.selected())){
//							setViewSelectListener(injectedSource,field,select.selected(),select.noSelected());
//						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void setListener(Object injectedSource, Field field,
			String methodName, Method method) throws Exception{
		if(methodName == null || methodName.trim().length() == 0)
			return;
		
		Object obj = field.get(injectedSource);
		
		switch (method) {
			case Click:
				if(obj instanceof View){
					((View)obj).setOnClickListener(new EventListener(injectedSource).click(methodName));
				}
				break;
			case Touch:
				if(obj instanceof View){
					((View)obj).setOnTouchListener(new EventListener(injectedSource).touch(methodName));
				}
				break;
			case ItemClick:
				if(obj instanceof AbsListView){
					((AbsListView)obj).setOnItemClickListener(new EventListener(injectedSource).itemClick(methodName));
				}
				break;
			case LongClick:
				if(obj instanceof View){
					((View)obj).setOnLongClickListener(new EventListener(injectedSource).longClick(methodName));
				}
				break;
			case ItemLongClick:
				if(obj instanceof AbsListView){
					((AbsListView)obj).setOnItemLongClickListener(new EventListener(injectedSource).itemLongClick(methodName));
				}
				break;
			default:
				break;
		}
	}
}
